//
//  VideoRecordProgressView.m
//  XPhoto
//
//  Created by sentinel on 2020/5/29.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "VideoRecordProgressView.h"

@implementation VideoRecordProgressView
- (void)setProgressList:(NSMutableArray *)progressList{
    _progressList = progressList;
    [self setNeedsDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGFloat lineWidth = 3.0f;
    CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    CGFloat radius = MIN(center.x, center.y) - lineWidth/2-1;
    
    CGFloat circumference = 2* M_PI *radius;
    CGFloat sepAngle = 2* M_PI * 3/circumference;
    CGFloat minAngle = 2* M_PI * 3/circumference;
    
    UIColor *progressColor = [UIColor orangeColor];
    UIColor *sepColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ctx, lineWidth);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:center];
    CGFloat currentAngle = -M_PI/2;
    for (NSInteger i = 0, n = self.progressList.count; i < n; i++) {
        if (i > 0) {

            [sepColor set];
            CGContextAddArc(ctx, center.x, center.y, radius, currentAngle, currentAngle + sepAngle, 0);
            CGContextDrawPath(ctx, kCGPathStroke);
            currentAngle += sepAngle;
        }
        CGFloat v = [[self.progressList objectAtIndex:i] floatValue];
        CGFloat angle = (v/self.maxValue) * 2 * M_PI;
        
        angle -= sepAngle;
        angle = MAX(angle, minAngle);
        
        [progressColor set];
        CGContextAddArc(ctx, center.x, center.y, radius, currentAngle, currentAngle + angle, 0);
        CGContextDrawPath(ctx, kCGPathStroke);
        currentAngle += angle;
    }
}


@end
